<template>
    <view class="pub_page pub_bind_account pub_page_bg pub_header_padding">
        <pub-head headerName="bind_account" :headerConfig="headerConfig"></pub-head>
        <pub-dialog v-show="dialogShow" :dialogConfig="dialogConfig" @dialogBack="dialogBack"></pub-dialog>
        <div class="pub_bind_account_box" v-if="pageLoading">
            <div class="pub_inputs">
                <div class="abox">
                    <div class="ali">
                        <div class="atitle">交易所账户</div>
                        <input class="ainput" type="text" placeholder="请输入交易所账户" v-model="account"
                            :disabled="!allowBind">
                        <div v-if="!allowBind" class="ainput_right_btn" @click="$comFun.toCopy(account)">复制</div>
                    </div>
                    <div class="ali" v-if="allowBind">
                        <div class="atitle">二级密码</div>
                        <input class="ainput" type="password" placeholder="请输入二级密码" v-model="password" maxlength="6">
                    </div>
                </div>
            </div>
            <br>
            <br>
            <br>
            <br>
            <br>
            <br>
            <div v-if="allowBind">
                <div class="pub_btn" @click="toDeal('to_bind')">确认绑定</div>
                <div class="txt">
                    <div class="txt_1">说明：</div>
                    <div class="txt_2">绑定交易所账户需核对自己账户，一旦绑定无法跟换，并赠送的旺票无法获得。</div>
                </div>
            </div>
        </div>
    </view>
</template>

<script>
export default {
    data() {
        return {
            headerConfig: {
                header_color: '#fff',
                title: '交易中心',//标题栏内容
                title_color: '#000',
                left_type: 3,//左侧区域类型  0不出现  1文字  2图片  3返回按钮
                left_content: 'dark',
            },

            dialogShow: false,
            dialogType: '',
            dialogConfig: {
                title: '提示',//标题栏内容
                content: ''
            },

            allowBind: true,
            pageLoading: false,
            account: '',
            password: ''
        };
    },
    onLoad() {
        this.getUserInfo()
    },
    methods: {
        dialogBack(data) {
            // console.log(data);
            this.dialogShow = false;
            if (data.key == 1) {
                this.toDeal(this.dialogType)
            }
        },
        async getUserInfo() {
            this.$comFun.openLoading();
            const res = await this.$request.get('user/userInfo', {});
            // console.log(res);
            this.$comFun.closeLoading();
            if (res.code == 200) {
                if (res.result.account) {
                    this.allowBind = false
                    this.account = res.result.account;
                    this.password = res.result.password;
                }
                this.pageLoading = true
            }
        },
        async toDeal(type, data_1, data_2) {
            switch (type) {
                case 'to_bind':
                    if (!this.account) {
                        this.$comFun.toast('请输入交易所账户');
                        return;
                    }
                    if (!this.password) {
                        this.$comFun.toast('请输入二级密码');
                        return;
                    }
                    this.dialogType = 'sure_bind';
                    this.dialogConfig = {
                        title: '提示',//标题栏内容
                        content: '是否确定绑定？'
                    }
                    this.dialogShow = true;
                    break;
                case 'sure_bind':
                    this.$comFun.openLoading();
                    const res = await this.$request.post('user/bindAccount', {
                        account: this.account,
                        password: this.password
                    });
                    this.$comFun.closeLoading();
                    this.$comFun.toast(res.message);
                    if (res.code == 200) {
                        this.allowBind = false
                    }
                    break;
                default:
                    break;
            }
        },
    },
};
</script>
<style lang="scss" scoped>
@import "@/common/css/mine.scss";
</style>
